<?php

abstract class Helper {

    public function addCss($filePath)
    {
        return '<link rel="stylesheet" href="'.$filePath.'">'."\r\n";
    }
    
    public function addFavicon($filePath)
    {
        return '<link rel="shortcut icon" href="'.$filePath.'"/>'."\r\n";
    }
    
    public function addJs($filePath)
    {
        return '<script src="'.$filePath.'"></script>'."\r\n";
    }
    
    public function renderView($viewName, $viewPath, $once = false) 
    {
        $file = $viewPath . '/' .$viewName. '.php';
        if (file_exists($file)) {
            if ($once) {
                include_once $file;
            } else {
                include $file;
            }
        }
    }
    
    public function trimAt($string, $trimAt = 20, $trimWords = 2)
    {
        if (strlen($string) > $trimAt) {
            $string = self::trimWords($string, $trimWords);
            if (strlen($string) > $trimAt) {
                $string = self::trimWords($string, --$trimWords) . ' ...';
            }
        }
        return $string;
    }
    
    public function trimWords($string, $words) {
        return implode(' ', array_slice(explode(' ', $string), 0, $words));
    }
    
    public function getPost($param = null, $default = null, $checkToken = true)
    {
        if ($_POST) {
            if ($param) {
                if (isset($_POST[$param])) {
                    return $_POST[$param];
                } 
                return $default;
            }
           return $_POST;
        }
    }
    
    public function makeToken()
    {
        return md5(microtime());
    }
    
    public function requestToken($sessionObj)
    {
        $token = self::makeToken();
        $sessionObj->setToken($token);
    }
    
    public function isValidToken($token, $sessionObj)
    {
        return $sessionObj->getToken() == $token;
    }   
    
}